﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test710.Models;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Web.Helpers;
namespace Test710.Controllers
{
    public class PlayerController : Controller
    {
        private FmisEntities db = new FmisEntities();

        //
        // GET: /Player/

        public ActionResult Index()
        {
            var players = db.Players.Include(p => p.Agent).Include(p => p.Club);
            return View(players.ToList());
        }

        //
        // GET: /Player/Details/5

        public ActionResult Details(int id = 0)
        {
            Player player = db.Players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // GET: /Player/Create

        public ActionResult Create()
        {
            ViewBag.AgentId = new SelectList(db.Agents, "AgentId", "FistName");
            ViewBag.ClubId = new SelectList(db.Clubs, "ClubId", "ClubName");
            return View();
        }

        //
        // POST: /Player/Create

        [HttpPost]
        public ActionResult Create(Player player, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null && (file.ContentType.Equals("image/bmp") || file.ContentType.Equals("image/jpeg") || file.ContentType.Equals("image/png") || file.ContentType.Equals("image/gif") || file.ContentType.Equals("image/tiff")))
                {

                    string extension = Path.GetExtension(file.FileName);
                    var newname = System.Guid.NewGuid().ToString("N");
                    file.SaveAs(HttpContext.Server.MapPath("~/ProfileImages/")
                                                          + newname + extension);

                    player.PlayerImage = newname + extension;

                }
                else
                {
                    return View("duoi cua file khong hop le");
                }
                db.Players.Add(player);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.AgentId = new SelectList(db.Agents, "AgentId", "FistName", player.AgentId);
            ViewBag.ClubId = new SelectList(db.Clubs, "ClubId", "ClubName", player.ClubId);
            return View(player);
        }

        //
        // GET: /Player/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Player player = db.Players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            ViewBag.AgentId = new SelectList(db.Agents, "AgentId", "FistName", player.AgentId);
            ViewBag.ClubId = new SelectList(db.Clubs, "ClubId", "ClubName", player.ClubId);
            return View(player);
        }

        //
        // POST: /Player/Edit/5

        [HttpPost]
        public ActionResult Edit(Player player,HttpPostedFileBase file,int id =0)
        {
            if (ModelState.IsValid)
            {
                if (file != null && (file.ContentType.Equals("image/bmp") || file.ContentType.Equals("image/jpeg") || file.ContentType.Equals("image/png") || file.ContentType.Equals("image/gif") || file.ContentType.Equals("image/tiff")))
                {
                    player = db.Players.Find(id);
                    string path = Server.MapPath("~/ProfileImages/" + player.PlayerImage);
                    FileInfo fi = new FileInfo(path);
                    if (fi.Exists)
                        fi.Delete();
                    string extension = Path.GetExtension(file.FileName);
                    var newname = System.Guid.NewGuid().ToString("N");
                    file.SaveAs(HttpContext.Server.MapPath("~/ProfileImages/")
                                                          + newname + extension);

                    player.PlayerImage = newname + extension;
                }
                db.Entry(player).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AgentId = new SelectList(db.Agents, "AgentId", "FistName", player.AgentId);
            ViewBag.ClubId = new SelectList(db.Clubs, "ClubId", "ClubName", player.ClubId);
            return View(player);
        }

        //
        // GET: /Player/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Player player = db.Players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // POST: /Player/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Player player = db.Players.Find(id);
            string path = Server.MapPath("~/ProfileImages/" + player.PlayerImage);
            FileInfo fi = new FileInfo(path);
            if (fi.Exists)
                fi.Delete();
            db.Players.Remove(player);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}